package com.ews.onlineexchange.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SmsSender {
	private static final String ACCESS_KEY_ID = "LTAIheELjArTrq0L";
	private static final String ACCESS_SECRET = "6DDZ2e4nQaVI7zgkP5sb8kNvOcjco8";
	private static final String SIGN_NAME = "东营市行政审批服务局";
	private static final String REGION_ID = "hangzhou";
	 public static String sendSms(String phone,String templateCode,String params) {
	    	String envSendSms = System.getenv("sendSms");
			if (envSendSms == null || !envSendSms.equals("true"))return "";
		 	DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
	        IAcsClient client = new DefaultAcsClient(profile);
	        CommonRequest request = new CommonRequest();
	        request.setMethod(MethodType.POST);
	        request.setDomain("dysmsapi.aliyuncs.com");
	        request.setVersion("2017-05-25");
	        request.setAction("SendSms");
	        request.putQueryParameter("PhoneNumbers", phone);
	        request.putQueryParameter("RegionId", REGION_ID);
	        request.putQueryParameter("SignName", SIGN_NAME);
	        request.putQueryParameter("TemplateCode", templateCode);
	       if(StringUtils.isNoneBlank(params)) request.putQueryParameter("TemplateParam", params.toString());
	        try {
	            CommonResponse response = client.getCommonResponse(request);
	            return response.getData();
	        } catch (ServerException e) {
	            e.printStackTrace();
	            return "error";
	        } catch (ClientException e) {
	            e.printStackTrace();
	            return "error";
	        }
	 }

	public static void main(String[] args) {
		JSONObject params = new JSONObject();
		params.put("sb", "test公司");
		params.put("projectName", "xx项目");
		String result = SmsSender.sendSms( "17660418726",SmsTemplateMapUtils.AUDIT_REJECT_TEMPLATE,params.toString());
		System.out.println(result);
	}
}
